<?php
/*
Template Name: Contact
*/
?>
<?php
if(isset($_POST['submitted'])) {
	if(trim($_POST['contactName']) === '') {
		$hasError = true;
	} else {
		$name = trim($_POST['contactName']);
	}

	if(trim($_POST['email']) === '')  {
		$hasError = true;
	} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
		$emailError = '您的Email地址格式不正确。';
		$hasError = true;
	} else {
		$email = trim($_POST['email']);
	}
	
	if(trim($_POST['subject']) === '') {
		$hasError = true;
	} else {
		$subject = trim($_POST['subject']);
	}

	if(trim($_POST['comments']) === '') {
		$hasError = true;
	} else {
		if(function_exists('stripslashes')) {
			$comments = stripslashes(trim($_POST['comments']));
		} else {
			$comments = trim($_POST['comments']);
		}
	}
	
	

	if(!isset($hasError)) {
		if (!isset($emailTo) || ($emailTo == '') ){
			$emailTo = get_option('admin_email');
		}
		$body = "姓名: $name \n\nEmail: $email \n\n内容: $comments";
		$headers = 'From: '.$emailTo . "\r\n" . 'Reply-To: ' . $email;

		$emailSent = mail($emailTo, $subject, $body);
	}

}?>
<?php get_header();?>
<div id="content">
<div id="mcontent" class="singular">
<div class="path">使用中国邮政Express给我发信</div>
<div class="entry">
<?php if( have_posts() ): while( have_posts() ): the_post();?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<h2>请填写如下信息给我发送邮件。</h2>
<?php if(isset($emailSent) && $emailSent == true){ ?>
<span style="color:green;">您的邮件发送成功</span><br />
<?php }elseif(isset($_POST['submitted'])){?>
<span style="color:red;">请检查填写您的Email地址</span><br />
<span style="color:red;">请检查您的必填项是否正确，然后重试</span><br />
<?php }elseif(isset($emailSent) && $emailSent == false) { ?>
<span style="color:red;">可能系统忙，请稍候再重试</span><br />
<?php }?>
<form action="<?php the_permalink(); ?>" id="contactForm" method="post">
<p>
<label for="contactName">姓名:</label>
<input name="contactName" class="input_tx" id="contactName" value="<?php echo $_POST['contactName'];?>" type="text" />
</p>
<p>
<label for="email">Email:</label>
<input name="email" class="input_tx" id="email" value="<?php echo $email;?>" type="text" />
</p>
<p>
<label for="subject">主题：</label>
<input name="subject" class="input_tx" id="subject" value="<?php echo $subject;?>" type="text" />
</p>
<p>
<label for="commentsText">消息:</label>
<textarea name="comments" class="input_ta" id="commentsText" rows="5" cols="20"><?php echo $comments;?></textarea>
</p>
<p>
<input type="submit" class="input_btn" value="按完请松手" />
</p>
<input name="submitted" id="submitted" value="true" type="hidden">
</form>

</div>
<?php endwhile;endif;?>
</div><!--end entry-->
<div class="clear"></div>
</div>
<!--end mcontent-->
<div id="lcontent">
<?php get_template_part('sidebar_l','single');?>
</div>
<div id="rcontent">
<?php get_template_part('sidebar_r','single');?>
</div>
</div>
<?php get_footer();?>
